package io.renren.modules.app.controller;

import cn.hutool.poi.excel.ExcelUtil;
import io.renren.common.utils.EasyPoiUtils;
import io.renren.modules.app.entity.CustomerEntity;
import io.renren.modules.app.service.CustomerService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 *
 * @author MR.XU
 * @email 260139486@qq.com
 * @date 2021-04-23 15:59:47
 */
@RestController
@RequestMapping("/api/excel")
@Api(value = "excel导入导出", tags = "excel导入导出", description = "excel导入导出")
public class ExcelController{

    @Autowired
    CustomerService customerService;

    /**
     * @description: 导出用户Excel
     * @auther: create by Administrator - MR.XU email:260139486@qq.com
     * @date 2021-04-23 15:59:47
     * @param
     * @return
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        List<CustomerEntity> resultList = customerService.list();
        long t1 = System.currentTimeMillis();
        EasyPoiUtils.exportExcel(resultList, "导出用户数据", "用户数据", CustomerEntity.class, "导出用户数据.xls", response);
        long t2 = System.currentTimeMillis();
        System.out.println(String.format("write over! cost:%sms", (t2 - t1)));
    }

    /**
     * @description: 导入用户Excel
     * @auther: create by Administrator - MR.XU email:260139486@qq.com
     * @date 2021-04-23 15:59:47
     * @param
     * @return
     */
    @PostMapping("/readExcel")
    public void readExcel(MultipartFile file){
        long t1 = System.currentTimeMillis();
        List<CustomerEntity> personList = EasyPoiUtils.importExcel(file, CustomerEntity.class);
        long t2 = System.currentTimeMillis();
        System.out.println(String.format("read over! cost:%sms", (t2 - t1)));
        System.out.println(personList.toString());
        customerService.saveBatch(personList);
    }
}